package main

import (
	"fmt"
	"sync"
)

type V struct {
	Value   int // 0
	Version int // 0,1,2
}

var mu sync.Mutex
var value V = V{
	Value:   0,
	Version: 0,
}

func a(version int, w *sync.WaitGroup) {
	for value.Value < 100 {
		mu.Lock()
		if value.Version == version {
			fmt.Printf("goroutine:%d, value:%d\n", version, value.Value)
			value.Value++
			value.Version = (value.Version + 1) % 3
		}
		mu.Unlock()
	}
	w.Done()
}

func main() {
	wait := sync.WaitGroup{}
	wait.Add(3)
	go a(0, &wait)
	go a(1, &wait)
	go a(2, &wait)
	wait.Wait()
}
